import { Component, OnInit,Input } from '@angular/core';
import {Hero} from "../hero";
import {HeroService} from "../hero.service";
import {ActivatedRoute,ParamMap} from "@angular/router";
import {Location} from "@angular/common";
import "rxjs/add/operator/switchMap";

@Component({
  selector: 'hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css'],
  providers:[HeroService]
})
export class HeroDetailComponent implements OnInit {

  constructor(private heroService:HeroService,private route:ActivatedRoute,private location: Location) { }

  ngOnInit():void {
    this.route.paramMap
      .switchMap((params: ParamMap) => this.heroService.getHeroById(+params.get('id')))
      .subscribe(hero => this.hero = hero);
  }
  @Input()
  hero: Hero;
  goBack():void{
    this.location.back();
  }
}
